import os
from loguru import logger


class OthersMethod:
    def yml_modify(self, data):
        """修改data文件夹中所有yml中的对应数据"""
        BASE_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
        data_path = os.path.join(BASE_PATH, "data")
        files = []
        from pathlib import Path
        for p in Path(data_path).iterdir():
            if str(p)[-3:] == 'yml':
                files.append(p)
            else:
                files.extend(p.rglob('*.yml'))
        if data:
            for file in files:
                file = str(file)
                if file[-3:] == 'yml':
                    for pro_old, pro_new in data.items():
                        with open(file, 'r', encoding='utf-8') as f:
                            yml_content = f.read()
                        f.close()
                        if str(pro_old) in yml_content:
                            new_content = yml_content.replace(str(pro_old), str(pro_new))
                            with open(file, 'w+', encoding='utf-8') as f:
                                f.write(new_content)
                            f.close()
                            print('yml文件内容更新成功！yml文件：{}。更新前后促销id分别为：{}-->{}'.format(file, pro_old,
                                                                                                       pro_new))
        else:
            print('未复制任何促销，所以未修改任何yml文件内容。')

    def log_print(self, log_all):
        """日志打印"""
        for logs in log_all:
            for key, value in logs.items():
                if key == 'info':
                    logger.info(value)
                elif key == 'error':
                    logger.error(value)


def docstring_parameter(*sub):
    """写一个可以添加变量注释的装饰器"""

    def dec(obj):
        obj.__doc__ = obj.__doc__.format(*sub)
        return obj

    return dec


if __name__ == '__main__':
    others_method = OthersMethod()
    data = {2021070100000051: 2021070100000050}
    others_method.yml_modify('功能测试/促销2_0/fetchProData', data)
